package com.qimou.iems.controller;

import com.qimou.iems.entity.Company;
import com.qimou.iems.entity.PageArgs;
import com.qimou.iems.entity.PageList;
import com.qimou.iems.service.ICompanyService;
import com.qimou.iems.util.CommonUtil;
import com.qimou.iems.util.StatusConstant;
import com.qimou.iems.util.ViewData;
import com.qimou.iems.util.ViewDataPage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Qimou Xie
 * @date 2018/3/14 22:11.
 **/

@RestController
@RequestMapping(value = "/company")
public class CompanyController extends BaseController {


    @Resource
    private ICompanyService companyService;


    @RequestMapping(value = "/list")
    public ViewDataPage list(PageArgs pageArgs,String companyName){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("companyName", CommonUtil.isEmpty(companyName) ? null : companyName);
        PageList<Company> list = companyService.getCompany(pageArgs, map);
        return buildSuccessViewDataPage(StatusConstant.SUCCESS_CODE,"获取成功",
                list.getTotalSize(),list.getList());
    }


    @RequestMapping(value = "/allCompany")
    public ViewData allCompany(){
        return buildSuccessJson(StatusConstant.SUCCESS_CODE,"获取成功",
                companyService.getAllCompany());
    }




    @RequestMapping(value = "/updateCompany")
    public ViewData updateCompany(Company c){
        if(null == c.getId()){
            return buildFailureJson(StatusConstant.FIELD_NOT_NULL,"参数错误");
        }
        companyService.update(c);
        return buildSuccessCodeJson(StatusConstant.SUCCESS_CODE,"操作成功");
    }


    @RequestMapping(value = "/save")
    public ViewData save(Company c){
        c.setParentId(1);
        companyService.save(c);
        return buildSuccessCodeJson(StatusConstant.SUCCESS_CODE,"操作成功");
    }


}
